package univer.oko.gui;

import univer.oko.utilities.CDUtilities;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Vector;

public class CDComboBox extends JComboBox {
    public CDComboBox() {
        super();
        this.setFocusable(false);
        this.setMaximumRowCount(6);
        this.setFont(CDUtilities.SYSTEM_FONT.deriveFont(12.0f));
    }

    public CDComboBox(int width, int height, int x, int y) {
        this();
        this.setBounds(x, y, width, height);
    }

    public void addItems(Vector v) {
        Object[] array = v.toArray();
        this.addItems(array);
    }

    public void addItems(ArrayList<?> v) {
        Object[] array = v.toArray();
        this.addItems(array);
    }

    public void addItems(Object[] a) {
        Arrays.sort(a);
        for (int i = 0; i < a.length; i++) {
            this.addItem(a[i]);
        }
    }

    public boolean isEmpty() {
        return this.getSelectedItem().toString().trim().equals("");
    }

    /**
     * This function in order to achieve more performance
     * make combobox disabled by adding an empty string, making it selected and then
     * disabling combo
     */
    public void makeDisable() {
        this.addItem("");
        this.setSelectedItem("");
        this.setEnabled(false);
    }

    /**
     * This function is reverse to previous one
     */
    public void makeEnabled() {
        this.removeItem("");
        this.setEnabled(true);
        this.setSelectedIndex(0);
    }
}
